# for test libcellex dll
# 2024-03-01 16:13:02  wangfeng
import time
from ctypes import *
import sys


class VerticeInfo(Structure):
    _fields_ = (
        ('id', c_int),
        ('point', POINTER(c_double))
    )


class FaceInfo(Structure):
    _fields_ = (
        ('id', c_int),
        ('vertice_size', c_int),
        ('vertice_index', POINTER(c_int)),
    )


class OffInfo(Structure):
    _fields_ = (
        ('vertices', c_int),
        ('faces', c_int),
        ('edges', c_int),
        ('v_info', POINTER(VerticeInfo)),
        ('f_info', POINTER(FaceInfo))
    )


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    dllobj = CDLL('../bin/libcellex.dll')

    off_info = pointer(OffInfo())
    dllobj.libcellex_init(byref(off_info))

    # 默认OFF文件
    filename = b'../bin/t3.off'

    try:
        # sys.argv[1] maybe null
        filename = bytes(sys.argv[1], 'UTF-8')
    except:
        pass

    dllobj.read_off(filename)

    print('\nthis is a demo for python import dll\n')
    print('vertices: %d, faces: %d, edges: %d' % (off_info.contents.vertices, off_info.contents.faces,
                                                  off_info.contents.edges))

    p1 = off_info.contents
    # 遍历顶点
    for index in range(p1.vertices):
        p2 = p1.v_info[index]
        print('vid:%d x=%f y=%f z=%f' % (p2.id, p2.point[0], p2.point[1], p2.point[2]), end='\n')

    # 遍历面
    for i in range(p1.faces):
        pf = p1.f_info[i]
        print('v_size:%d' % pf.vertice_size, end='')
        for j in range(pf.vertice_size):
            print(', index=%d' % pf.vertice_index[j], end='')
        print('')

    time.sleep(1)

    dllobj.libcellex_free(byref(off_info))

# 观察exit code是否为0
